package com.mmf.ancientcostume.adapter.goods;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.activity.ImagePreviewActivity;
import com.mmf.ancientcostume.adapter.home.ReleaseInfoImageAdapter;
import com.mmf.ancientcostume.base.adapter.BaseClickRecyclerAdapter;
import com.mmf.ancientcostume.common.utils.DipUtil;
import com.mmf.ancientcostume.common.utils.service.SecretConstant;
import com.mmf.ancientcostume.model.Comment;
import com.mmf.ancientcostume.model.StaticData;
import com.mmf.ancientcostume.widget.CircleTransform;
import com.mmf.ancientcostume.widget.ReleaseAdapterInterface;
import com.mmf.ancientcostume.widget.SpaceItemDecoration;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by MMF
 * date 2017/10/23
 * Description:评论列表适配器
 */
public class CommentListAdapter extends BaseClickRecyclerAdapter<Comment> implements View.OnClickListener {
    Picasso picasso;

    public CommentListAdapter(Context context) {
        super(context);
        picasso = Picasso.with(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_comment_list, parent, false);
        ViewHolder mHolder = new ViewHolder(view, context);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.lytCommentList.setOnClickListener(this);
        viewHolder.lytCommentList.setTag(position);
        final Comment item = itemList.get(position);
        picasso.load(SecretConstant.LOAD_IMG + item.getHead())
                .noFade()
                .placeholder(R.drawable.pictures_no)
                .error(R.drawable.pictures_no)
                .transform(new CircleTransform())
                .into(viewHolder.ivHome);
        viewHolder.tvName.setText(item.getName());
        viewHolder.tvContent.setText(item.getContent());
        if (item.getImgList() != null && item.getImgList().size() > 0) {
            viewHolder.rvPreviewImg.setVisibility(View.VISIBLE);
            viewHolder.adapter.setItems(item.getImgList());
            viewHolder.rvPreviewImg.setAdapter(viewHolder.adapter);
            viewHolder.adapter.setReleaseAdapterInterface(new ReleaseAdapterInterface() {
                @Override
                public void jumpSelImage() {
//                    jumpSelPhoto(StaticData.RELEASE_SEL_IMAGE);
                }

                @Override
                public void jump(int position, List<String> itemList) {
//                    Intent intent = new Intent(context, ImagePreviewActivity.class);
//                    intent.putExtra("type", "1");
//                    intent.putExtra("selPosition", position);
//                    intent.putStringArrayListExtra("imgUrls", (ArrayList<String>) imgUrls);
//                    intent.putStringArrayListExtra("imgPath", (ArrayList<String>) itemList);
//                    startActivityForResult(intent, 1);
                }
            });
        } else {
            viewHolder.rvPreviewImg.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取position
            onItemClickListener.onItemClick(view, (int) view.getTag(),itemList.get((int) view.getTag() ));
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_home)
        ImageView ivHome;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.rv_preview_img)
        RecyclerView rvPreviewImg;
        @BindView(R.id.lyt_comment_list)
        LinearLayout lytCommentList;
        CommentImageAdapter adapter;

        ViewHolder(View view, Context context) {
            super(view);
            ButterKnife.bind(this, view);

            //将RecyclerView设置成gridview的样式 每行显示4个
            GridLayoutManager mgr = new GridLayoutManager(context, 3);
            int space = DipUtil.dip2px(context, 4);
            //设置显示的适配器
            adapter = new CommentImageAdapter(context, 3);
            rvPreviewImg.setLayoutManager(mgr);
            //设置RecyclerView每个item的间距
            rvPreviewImg.addItemDecoration(new SpaceItemDecoration(space, 3));
        }
    }

}
